const http = require('../../utils/http.js');
const utils = require('../../utils/util.js');
Component({
  properties: {
    background: {
      type: String,
      value: 'rgba(255, 255, 255, 1)'
    },
    color: {
      type: String,
      value: 'rgba(0, 0, 0, 1)'
    },
    titleText: {
      type: String,
      value: '导航栏'
    },
    backIcon: {
      type: String,
      value: ''
    },
    homeIcon: {
      type: String,
      value: ''
    },
    fontSize: {
      type: Number,
      value: 16
    },
    iconHeight: {
      type: Number,
      value: 30
    },
    iconWidth: {
      type: Number,
      value: 30
    },
    titleIcon: {
      type: String,
      value: ''
    }
  },
  data: {
    rotate: 0,
    active: 0,
    titleList: '',
    titleText: '',
    pages: 0,
    group_id: 0
  },
  attached: function () {
    var that = this;
    that.setNavSize();
    that.setStyle();
  },
  methods: {
    // 通过获取系统信息计算导航栏高度
    setNavSize: function () {
      var that = this,
        sysinfo = wx.getSystemInfoSync(),
        statusHeight = sysinfo.statusBarHeight,
        isiOS = sysinfo.system.indexOf('iOS') > -1,
        navHeight;
      if (!isiOS) {
        navHeight = 48;
      } else {
        navHeight = 44;
      }
      that.setData({
        status: statusHeight,
        navHeight: navHeight
      })
    },
    setStyle: function () {
      var that = this,
        containerStyle, textStyle, iconStyle;
      containerStyle = [
        'background:' + that.data.background
      ].join(';');
      textStyle = [
        'color:' + that.data.color,
        'font-size:' + that.data.fontSize + 'px'
      ].join(';');
      iconStyle = [
        'width: ' + that.data.iconWidth + 'px',
        'height: ' + that.data.iconHeight + 'px'
      ].join(';');
      that.setData({
        containerStyle: containerStyle,
        textStyle: textStyle,
        iconStyle: iconStyle
      })
    },
    // 返回事件
    back: function () {
      wx.navigateBack({
        delta: 1
      })
    },
    home: function () {
      wx.reLaunch({
        url: '/pages/onNewPages/index/index',
      })
    },
    onTitleClick() {
      var _this = this
      console.log(this)
      _this.setData({
        rotate: _this.data.rotate == 0 ? -180 : 0
      })
      var data = {
        status: 0,
        now_page: 0
      }
      http.getRequest('v1/orderGroup', data,
        function (res) {
          console.log(res)
          _this.setData({
            titleList: res.data
          })
        },
        function (err) {})
    },
    onTaoOrder(e) {
      var _this = this,
        id = e.currentTarget.id
      _this.setData({
        active: id,
        titleText: e.currentTarget.dataset.name,
        rotate: 0,
        group_id: e.currentTarget.dataset.id
      })
      _this.onOrderList(id, e.currentTarget.dataset.id)
    },
    onOrderList(id, e) {
      console.log('>>>>>>>>>>>>>>>>>>>>>', id, e)
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 1]
      var _this = this,
        data = {
          status: id ? id : _this.data.active,
          now_page: _this.data.pages,
          group_id: e ? e : _this.data.group_id
        }
      http.getRequest('v1/profit', data,
        function (res) {
          console.log(res)
          wx.hideToast()
          for (let i in res.data.list) {
            res.data.list[i].pay_time = utils.formatTime(res.data.list[i].pay_time, 'Y-M-D h:m:s')
          }
          prevPage.setData({
            orderList: res.data.list,
            tips: res.data.question,
            NetWorkStatus: true
          })
          console.log(prevPage, _this)
        },
        function (err) {})
    }
  }
})